require '../db_help/db_help'
require 'taobao_api'
FLITER=[21541,1625894,1625894,20509,1625899,1625894,20518,20509,1627207,31610,2396373]
def hash_to_str (h)
    val = ''
    h.each do |p|
      val += p.join(":")+";"
    end
    val.slice(0,val.length-1)
end

def transfer_prop_to_str_v2(dao,cid,props)
      str={}
      return if cid.nil?
      prop = props.split(";")
      prop.each do |s|
        values = s.split(":")
        next unless FLITER.index(values[0].to_i).nil?
        rs = dao.query("select pid,property_name,name from tao_property_values where pid=#{values[0]} and vid=#{values[1]} and cid=#{cid}")
        if !rs.nil? && rs.num_rows>0
          while rst = rs.fetch_hash do
            s = rst['property_name']
            #replace(rst['property_name'],p['input_pids'],p['input_str'])
            if str[s].nil?
              str[s]=rst['name']
            else
              str[s]=str[s]+","+rst['name']
            end
          end
        end
      end
      return hash_to_str(str)
end
def update_inputpids
    db_config1 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'tao_hidoudou'}
    tao_dao = DBHelp::DBHelp.out_initialize(db_config1)
    rs = tao_dao.query("select id,props_str,input_pids,input_str from tao_items where input_pids like '%20000%' limit 1")
    rs.each do |row|
        inputpids = row[2].to_s.split(",")
        brand_index = inputpids.index('20000')
        puts inputpids
        puts brand_index
        inputstr=  row[3].to_s.split(",")
        str = inputstr[brand_index]
        reg=Regexp.new("(品牌:.*;)")
        if !row[1].to_s.gsub!(reg,"品牌:#{str}").nil?
            props = row[1].to_s.gsub!(reg,"品牌:#{str}") 
            tao_dao.query("update tao_items set props_str='#{props}' where id=#{row[0]}")
        else
            props = row[1].to_s+";品牌:#{str}"
            tao_dao.query("update tao_items set props_str='#{props}' where id=#{row[0]}")
        end
    end
        
end

def update_props
db_config1 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'tao_hidoudou'}
tao_dao = DBHelp::DBHelp.out_initialize(db_config1)

props_is_null = tao_dao.query("select props,cat1_id,cat2_id,id,original_cid from tao_items where props_str='' and status>=0")
props_is_null.each do |row|
   puts "1---"+row[3]
   p_str = transfer_prop_to_str_v2(tao_dao,row[1],row[0])
   puts p_str
   if p_str.nil?
      puts "2---"
      p_str = transfer_prop_to_str_v2(tao_dao,row[2],row[0]) 
      puts p_str
   end
   if p_str.nil?
      puts "3---"
      p_str = transfer_prop_to_str_v2(tao_dao,row[4],row[0]) 
      puts p_str
   end
   
   tao_dao.query("update tao_items set props_str='#{p_str}',updated_at='#{TaobaoAPI.timestamp}' where id=#{row[3]}")
end
end
#update_inputpids
=begin
db_config1 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'tao_hidoudou'}
tao_dao = DBHelp::DBHelp.out_initialize(db_config1)
rs = tao_dao.query("select id,props_str from tao_items where props_str like '%细节图:%'")
puts rs.num_rows
rs.each do |row|
    prop = row[1].to_s.gsub!("细节图:有细节图;","")
    tao_dao.query("update tao_items set props_str ='#{prop}' where id=#{row[0]}")
end
=end
db_config1 = {"host" => '118.126.2.138', 'encoding' => 'utf8', 'username' => 'root', 'password' => 'admin', 'database' => 'tao_hidoudou'}
tao_dao = DBHelp::DBHelp.out_initialize(db_config1)
rs = tao_dao.query("select id,original_cid,props from tao_items where props_str =''")
rs.each do |row|
ps = transfer_prop_to_str_v2(tao_dao,row[1].to_s,row[2].to_s)
tao_dao.query("update tao_items set props_str='#{ps}' where id=#{row[0]}")
end

